package com.huanwei.cc.db;

import android.content.Context;

import com.huanwei.cc.bean.Life_MSG2;
import com.j256.ormlite.dao.Dao;
import com.huanwei.cc.bean.Life_Login_UserInfo;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.util.List;


/**
 * Created by luoliwen on 16/4/28.
 * 完成对数据库的CRUD的操作
 */
public class Z_Login_UserInfoDao {
    private Context context;
    private Dao<Life_Login_UserInfo, Integer> userDao;
    private Z_DataBaseHelper helper;

    public Z_Login_UserInfoDao(Context context) {
        this.context = context;
        helper = Z_DataBaseHelper.getInstance(context);
        try {
            userDao = helper.getDao(Life_Login_UserInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addUser(String name,String account, String password
    ) {
        try {
            Life_Login_UserInfo user = new Life_Login_UserInfo();
            user.setId(String.valueOf(Math.random() * 10));
            user.setName(name);
            user.setAccount(account);
            user.setPassword(password);
            user.setJinyan("N");
            user.setJieshao("我们都是好孩子！");
            userDao.create(user);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public boolean shifoucunzai(String account) {
        try {
            List<Life_Login_UserInfo> account1 = userDao.queryBuilder().where().eq("account", account).query();
            if (account1.size() > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String getpassword(String account) {
        try {
            Life_Login_UserInfo account1 = userDao.queryBuilder().where().eq("account", account).query().get(0);
            return account1.getPassword();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public String getname(String account) {
        try {
            Life_Login_UserInfo account1 = userDao.queryBuilder().where().eq("account", account).query().get(0);
            return account1.getName();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public String getjinyan(String name) {
        try {
            Life_Login_UserInfo account1 = userDao.queryBuilder().where().eq("name", name).query().get(0);
            return account1.getJinyan();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    List<Life_Login_UserInfo> XTWUserInfos;

    public List<Life_Login_UserInfo> query() {
        try {
            XTWUserInfos = userDao.queryForAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return XTWUserInfos;
    }
    public List<Life_Login_UserInfo> up(String jinyan,String id) {
        try {
            UpdateBuilder<Life_Login_UserInfo, Integer> life_devicesIntegerUpdateBuilder = userDao.updateBuilder();
            life_devicesIntegerUpdateBuilder.updateColumnValue("jinyan", jinyan).where().eq("id", id);
            life_devicesIntegerUpdateBuilder.update();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return XTWUserInfos;
    }


}
